
function plugin(Vue) {
  if (plugin.installed) {
    return
  }

  !Vue.prototype.$auth && Object.defineProperties(Vue.prototype, {
    $auth: {
      get() {
        const _this = this
        return (authPermission) => {
          // 当前组件需要的权限
          const [permission, action] = authPermission.split('.')

          // 用户具有的permission action
          const permissions = _this.$store.getters.permissions
          const actions = _this.$store.getters.actions

          let hasAction = false
          let hasPermission = false

          if (!permission || permission === '') {
          // 如果没有permission，不要求权限
            hasPermission = true
          } else {
            for (let i = 0; i < permissions.length; i++) {
              const item = permissions[i]
              if (item === permission) {
                hasPermission = true
                break
              }
            }
          }

          for (let i = 0; i < actions.length; i++) {
            const item = actions[i]
            if (item === action) {
              hasAction = true
              break
            }
          }

          return hasAction && hasPermission
        }
      }
    }
  })
}

export default plugin
